package cn.szzsi.model;

import cn.szzsi.model.base.BaseResource;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;

import java.util.ArrayList;
import java.util.List;

/**
 * 实体类 - 资源
 * 
 */
//@TableBind(tableName = "b_resource")
public class Resource extends BaseResource<Resource> {

	private static final long serialVersionUID = 7906172672735389314L;

	public static final String PATH_SEPARATOR = ",";// 树路径分隔符
	
	public static final Resource dao = new Resource().dao();

	public Resource getByUrl(String url){
		return dao.findFirst("select * from sys_resource where url=? and level='3'",url);
	}
	
	/**
	 * 获得所有资源
	 * @return
	 */
	public List<Resource> getAll(){
		return dao.find("select * from sys_resource");
	}

	/**
	 * 获取指定店铺的指定级别的资源
	 * @param shopId
	 * @param level
	 * @return
	 */
	public List<Resource> getByUidAndLevel(String shopId, int level){
		shopId = getInSql(shopId);
		return dao.find("select * from sys_resource where uid in "+shopId+" and level = ? order by orderList asc ",level);
	}

	/**
	 * 根据角色查看资源
	 * @param level
	 * @param member
	 * @return
	 */
	public List<Resource>getByshopIdAndLevelAndRoleId(String shopId,int level,Member member){
		if("super".equals(member.getRoleId())){
			shopId = getInSql(shopId);
			String sql="select * from sys_resource where uid in "+shopId+" and level = '"+level+"'";
			if(member.getType()==1){//商户-->看不到代理商的菜单
				sql+=" and roleType!=2";
			}else if(member.getType()==2){//代理商-->看不到商户的菜单
				sql+=" and roleType!=1";
			}
			sql+=" order by orderList asc";
			return dao.find(sql);
		}else{
			return dao.find("SELECT * FROM `sys_resource` where level = ? and id in(select resourceId from sys_role_resource where roleId=?) order by orderList asc",level,member.getRoleId());
		}

	}

	//获取用户自己加的资源和系统默认资源
	public List<Resource> getByUid(String uid){
		uid = getInSql(uid);
		String sql = "select * from sys_resource where uid in "+uid+" and level = 0 order by orderList asc ";
		return dao.find(sql);
	}

	//加载所有资源
	public List<Resource> getAllTree(){
		String sql = "select * from sys_resource where level !='0' order by orderList asc ";
		List<Resource> businessResources = dao.find(sql);
		return recursivResourceTreeList(businessResources, null);
	}
	//按照递归关系获取用户自己加的资源和系统默认资源
	public List<Resource> getTreeByUid(String uid){
		uid = getInSql(uid);
		String sql = "select * from sys_resource where uid in "+uid+"  order by orderList asc ";
		List<Resource> businessResources = dao.find(sql);
		return recursivResourceTreeList(businessResources, null);
	}

	private String getInSql(String uid){
		return "('"+uid+"','-1')";
	}

	public List<Resource> getByPid(String pid, String uid){
		if(StrKit.notBlank(pid)){
			return dao.find("select * from sys_resource where 1=1 and parent_id = ? and uid !='0' order by orderList asc",pid);
		}
		uid = getInSql(uid);
		return dao.find("select * from sys_resource where 1=1 and uid in "+uid+" and parent_id is null and level!='0' order by orderList asc");
	}

	public List<Resource> getByPidAndRoleId(String pid,Member member){
		if("super".equals(member.getRoleId())){
			String sql="select * from sys_resource where 1=1 and parent_id = '"+pid+"' and uid !='0' ";
			if(member.getType()==1){//商户-->看不到代理商的菜单
				sql+=" and roleType!=2";
			}else if(member.getType()==2){//代理商-->看不到商户的菜单
				sql+=" and roleType!=1";
			}
			sql+=" order by orderList asc";
			return dao.find(sql);
		}else{
			return dao.find("select * from sys_resource where 1=1 and parent_id = ? and uid !='0' and id in(select resourceId from sys_role_resource where roleId=?) order by orderList asc",pid,member.getRoleId());
		}

	}

	public List<Resource> getByPidOrCid(String pid, String uid){
		if(StrKit.notBlank(pid)){
			return dao.find("select * from sys_resource where 1=1 and path like '%"+pid+"%' and path!=?",pid);
		}
		uid = getInSql(uid);
		return dao.find("select * from sys_resource where 1=1 and uid in "+uid+" and parent_id is null");
	}

	// 获得子
	public List<Resource> getChildren() {
		List<Resource>resourceList= CacheKit.get("resourceList",getStr("id"));
		if(resourceList==null){
			resourceList=dao.find("select * from sys_resource t where t.parent_id = ? order by orderList asc ",getStr("id"));
			CacheKit.put("resourceList",getStr("id"),resourceList);
		}
		return resourceList;
	}

	//是否有子节点
	public boolean hasChildren(String pid){
		List<Resource> lbr = dao.find("select * from sys_resource t where t.parent_id = ? order by orderList asc ",pid);
		if(lbr!=null&&lbr.size()>0){
			return true;
		}else{
			return false;
		}
	}

	// 获取分类的文章
	public List<Resource> getResourceList(){
		String sql = "select * from sys_resource where parent_id = ?";
		return Resource.dao.find(sql,getStr("id"));
	}

	/**
	 * 检查资源名是否存在
	 * @param
	 * @return
	 */
	public boolean checkName(String name) {
		return dao.findFirst("select name from sys_resource where name = ? limit 1", name) == null;
	}
	// 获得层
	public Integer getLevel() {
//		System.out.println(getStr("path"));
		return getStr("path").split(PATH_SEPARATOR).length - 1;
	}

	public Resource getParent() {
		return dao.findById(getStr("parent_id"));
	}
	/**
	 * 检查资源值是否存在
	 * @param 
	 * @return
	 */
	public boolean checkValue(String value) {
		return dao.findFirst("select value from sys_resource where value = ? limit 1", value) == null;
	}

	// 递归父类排序分类树
	private List<Resource> recursivResourceTreeList(List<Resource> businessResources, List<Resource> temp) {
		if (temp == null) {
			temp = new ArrayList<Resource>();
		}
		for (Resource businessResource : businessResources) {
			String parent = businessResource.getStr("parent_id");
			if(StrKit.isBlank(parent)){
				temp.add(businessResource);//顶级菜单
				for (Resource br : businessResources) {
					if(StrKit.notBlank(br.getStr("parent_id"))&&br.getStr("parent_id").equals(businessResource.getStr("id"))){
						temp.add(br);//二级菜单
						for(Resource br0:businessResources){
							if(StrKit.notBlank(br0.getStr("parent_id"))&& br0.getStr("parent_id").equals(br.getStr("id"))){
								temp.add(br0);//三级菜单
							}
						}

					}
				}

			}
		}
		return temp;
	}
}
